
import groovy.grape.Grape
Grape.grab(group:"org.apache.camel", module:"camel-core", version:"2.0.0")
Grape.grab(group:"org.apache.camel", module:"camel-jetty", version:"2.0.0")
Grape.grab(group:"org.apache.camel", module:"camel-freemarker", version:"2.0.0")

class HelloBean {
  public String sayHello(String name) {
    "Hello ${name}, nice going."
  }
  public String sayHelloToBoss(String name) {
    "Hello Boss."
  }
}

class MyRouteBuilder extends org.apache.camel.builder.RouteBuilder { 
  void configure() { 
    from("direct://foo").choice()
      .when(body().contains("Jack")).bean(HelloBean.class, "sayHelloToBoss")
      .otherwise().bean(HelloBean.class, "sayHello")
      .end().to("mock://result")
    from("direct://xmlfoo").transform().xpath("//user/@name", String.class).to("direct://foo")
	//curl -v -d "name=Mary&age=28" http://localhost:8080/tutorial
    from('jetty:http://localhost:8080/tutorial').transform()
	  .simple('<user name="${header.name}"><age>${header.age}</age></user>').to("direct://xmlfoo")
          .to("freemarker:hello.ftl")	
  }
}

ctx = new org.apache.camel.impl.DefaultCamelContext()
mrb = new MyRouteBuilder()
ctx.addRoutes mrb
ctx.start()
p = ctx.createProducerTemplate()

p.sendBody("direct:foo", "Jack")
p.sendBody("direct:xmlfoo", "<user name='Paul'><age>45</age></user>")
p.sendBody("direct:xmlfoo", "<user name='Jack'><age>42</age></user>")

e = ctx.getEndpoints("mock://result")[0]
e.exchanges.each { ex ->
  println "INFO> in.body='${ex.in.body}'"
}
